package com.rongshu.view.penpal.activity;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.api.RetorfitService;
import com.rongshu.utils.Common;
import com.rongshu.utils.Constant;
import com.rongshu.view.base.AdapterListener;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.discovery.adapter.SelectLabelAdapter;
import com.rongshu.view.home.bean.LabelResponse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Subscriber;
import rx.functions.Action0;

/**
 * created by zhengweis on 2018/11/8.
 * description：笔友会标签列表
 */
public class PenpalLabelActivity extends BaseActivity {
    @BindView(R.id.labels)
    RecyclerView labels;
    @BindView(R.id.layout_option)
    TextView option;
    @BindView(R.id.head_middle)
    TextView headMiddle;
    @BindView(R.id.head_right)
    TextView headRight;
    private SelectLabelAdapter adapter;
    private List<LabelResponse.ResponseBean> tagsList;
    private ArrayList<LabelResponse.ResponseBean> selectTags;
    private LabelResponse labelResponse;
    private int CREATE_PENPAL_SUCCESS = 101;

    @Override
    protected int initLayout() {
        return R.layout.activity_penpal_label;
    }

    @Override
    protected void initView() {
        headRight.setText("跳过");
        headRight.setTextColor(ContextCompat.getColor(this, R.color.color_3cc051));
        headMiddle.setText("选择笔友会标签");
        option.setEnabled(false);
        tagsList = new ArrayList<>();
        selectTags = new ArrayList<>();
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        labels.setLayoutManager(gridLayoutManager);
        adapter = new SelectLabelAdapter(this, tagsList);
        labels.setAdapter(adapter);
        adapter.setAdapterListener(new AdapterListener() {
            @Override
            public void callBack(Object object) {
                selectTags.clear();
                for (LabelResponse.ResponseBean bean : tagsList) {
                    if (bean.getIsSelected()) {
                        selectTags.add(bean);
                    }
                }
                option.setEnabled(selectTags.size() != 0);
                option.setBackgroundResource(selectTags.size() == 0 ? R.drawable.shape_btn_e5e5e5 : R.drawable.shape_btn_3cc051);
            }
        });
    }

    @Override
    protected void initData() {
        getPenpalLabels();
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == CREATE_PENPAL_SUCCESS) {
            finish();
        }
    }

    public void getPenpalLabels() {
        Map<String, Object> map = new HashMap<>();
        map.put("rnd", 0);
        RetorfitService.getPenpalLabels(0,
                Common.encryptionSign(map)).doOnSubscribe(new Action0() {
            @Override
            public void call() {

            }
        }).subscribe(new Subscriber<LabelResponse>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(LabelResponse object) {
                if (object != null && object.getResponse() != null) {
                    labelResponse = object;
                    tagsList.clear();
                    tagsList.addAll(object.getResponse());
                    adapter.notifyDataSetChanged();
                }
            }
        });
    }

    @OnClick({R.id.head_left, R.id.head_right, R.id.layout_option})
    public void onViewClicked(View view) {
        LabelResponse response = new LabelResponse();
        switch (view.getId()) {
            case R.id.head_left:
                finish();
                break;
            case R.id.head_right:
                response.setResponse(selectTags);
                Intent intentA = new Intent(this, NewPenpalActivity.class);
                intentA.putExtra(Constant.SELECT_LABEL, response);
                intentA.putExtra(Constant.LABEL, labelResponse);
                startActivityForResult(intentA, CREATE_PENPAL_SUCCESS);
                break;
            case R.id.layout_option:
                if (selectTags.size() > 4) {
                    toast("请最多选中四个标签");
                } else {
                    response.setResponse(selectTags);
                    Intent intentB = new Intent(this, NewPenpalActivity.class);
                    intentB.putExtra(Constant.SELECT_LABEL, response);
                    intentB.putExtra(Constant.LABEL, labelResponse);
                    startActivityForResult(intentB, CREATE_PENPAL_SUCCESS);
                }
                break;
        }
    }
}
